<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Payment extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($order_by='created_date', $sort_order='asc', $current_page=0){
			
			$results_per_page = 3;
		
		
			$url = CURL_URL;
			$data = array('www-command'=>'payment-all_payments', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			$data['payment_all_payments'] = $response;
			
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
		
			$this->load->library('pagination');			
			$config['base_url'] = 'http://localhost/azindia/azindia-client/index.php/admin/payment/index/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows'] = $response->total;
			$config['uri_segment'] = 6;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['sort_order'] = $sort_order;
			$data['order_by'] = $order_by;
			$data['payment_fields'] = array('payment_gateway'=>'Payment Gateway', 'admin_id'=>'Last Updated By', 'status'=>'Status', 'created_date'=>'Date');
			$this->load->view('admin/payment', $data);
 		}
		
		/* function create_payment_page(){
			$this->load->view('admin/create_payment_page');
		} */
		
		function create_payment(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('payment', 'Payment', 'required|alpha');
				$this->form_validation->set_rules('url', 'URL', 'callback_valid_url_format');
				$this->form_validation->set_rules('info', 'Other Info','max_length[500]');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/create_payment_page');
				}else{
					$url = CURL_URL;
					$data = array('www-command'=>'payment-create_payment', 
					'payment_gateway' => $this->input->post('payment'),
					'payment_url' => $this->input->post('url'),
					'payment_otherinfo' => $this->input->post('info'),
					'admin_id' => $this->session->userdata('admin_id')
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/payment/index/');
				}
			}
			
			function valid_url_format($url){
				if($url == ''){
					return true;
				}
			$pattern = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
			if(!preg_match($pattern, $url)){
				$this->form_validation->set_message('valid_url_format', 'The URL you entered is not accessible.');
				return false;
				}
				return true;
			}
			
			/*  function edit_payment_page(){
				$this->load->model('admin_payment_model');
				if($this->uri->segment(4) > 0){
					$_SESSION['payment_id'] = $this->uri->segment(4);
				}
					$data = array('www-command'=>'payment-payment_updates_list', 
					'payment_id' => $_SESSION['payment_id']
					);
					$update_list = $this->admin_payment_model->payment_list($data);
					$data = array('www-command'=>'payment-get_payment', 
					'payment_id' => $_SESSION['payment_id']
					);
					$payment_data = $this->admin_payment_model->payment_list($data);
					
					$data['update_list'] = $update_list;
					$data['payment_data'] = $payment_data;
					//echo '<pre>';print_r($data);exit;
					$this->load->view('admin/edit_payment_page', $data);
			}  */
			
			function edit_payment(){
				
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('payment', 'Payment', 'required|alpha');
				$this->form_validation->set_rules('url', 'URL', 'callback_valid_url_format');
				$this->form_validation->set_rules('info', 'Other Info','max_length[500]');
				
				
				if($this->form_validation->run() == FALSE){
				$this->load->model('admin_payment_model');
				if($this->uri->segment(4) > 0){
					$_SESSION['payment_id'] = $this->uri->segment(4);
				}
					$data = array('www-command'=>'payment-payment_updates_list', 
					'payment_id' => $_SESSION['payment_id']
					);
					$update_list = $this->admin_payment_model->payment_list($data);
					$data = array('www-command'=>'payment-get_payment', 
					'payment_id' => $_SESSION['payment_id']
					);
					$payment_data = $this->admin_payment_model->payment_list($data);
					
					$data['update_list'] = $update_list;
					$data['payment_data'] = $payment_data;
					//echo '<pre>';print_r($data);exit;
					$this->load->view('admin/edit_payment_page', $data);
				}else{
					if($this->input->post('status') == 'on'){
						$status = 1;
					}else{
						$status = 0;
					}
					
					$url = CURL_URL;
					$data = array('www-command'=>'payment-edit_payment', 
					'payment_gateway' => $this->input->post('payment'),
					'payment_url' => $this->input->post('url'),
					'payment_otherinfo' => $this->input->post('info'),
					'payment_id' => $_SESSION['payment_id'],
					'status' => $status,
					'admin_id' => $this->session->userdata('admin_id')
					);
					//echo '<pre>';print_r($this->session->userdata('admin_id'));exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/payment/index/');
				}
			}
			
			function delete_payment(){
				$url = CURL_URL;
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'payment-delete_payment','status'=>$status, 'payment_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/payment/index/');
			}
}